//
//  WCNCareSchemeListVC.m
//  WCN
//
//  Created by 杨鑫 on 15/7/1.
//  Copyright (c) 2015年 ShanghaiCize Trade And Business Co., Ltd. All rights reserved.
//

#import "WCNCareSchemeListVC.h"
#import "WCNCareSchemeVC.h"
//
#import "MJRefresh.h"
//
#import "WCPopupLayerManager.h"
#import "WCNetworkAccessManager.h"
#import "WCCoreDataStore.h"
//
#import "Constants.h"

@interface WCNCareSchemeListVC ()<UITableViewDataSource, UITableViewDelegate>

@property (strong, nonatomic) Patient *patient;

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIButton *addScheme;

@property (strong, nonatomic) NSString *url;
@property (strong, nonatomic) NSArray *schemes;

@end

@implementation WCNCareSchemeListVC

- (instancetype)initWithPatient:(id)patient
{
    self = [super init];
    if (self) {
        self.title = @"已选用方案";
        
        _patient = patient;
        _url = [NSString stringWithFormat:@"nursing/case/list/%@", self.patient.stringId];
        _schemes = [[WCCoreDataStore shareStore] getPatientSchemes:self.patient.stringId];
        
        [[NSNotificationCenter defaultCenter] addObserverForName:CareSchemeListRefreshNotification object:nil queue:nil usingBlock:^(NSNotification *note){
            if (![self.tableView.header isRefreshing]) {
                [self.tableView.header beginRefreshing];
            }
        }];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.addScheme.layer.cornerRadius = 2;
    
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
    
    __weak typeof(self) weakSelf = self;
    [self.tableView addLegendHeaderWithRefreshingBlock:^{
        [weakSelf refresh];
    }];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    if ([[WCNetworkAccessManager sharedMg] timeSinceLastUrl:self.url] > kUrlCD && ![self.tableView.header isRefreshing]) {
        [self.tableView.header beginRefreshing];
    }
}

- (void)refresh
{
    [[WCNetworkAccessManager sharedMg] POST:self.url parameters:@{@"status": @"IN_PROGRESS"} success:^(NSDictionary *result){
        self.schemes = [[WCCoreDataStore shareStore] createPatientSchemes:result[@"data"] withRecord:self.patient.stringId];
        [self.tableView reloadData];
    } failure:nil netError:nil always:^{
        [self.tableView.header endRefreshing];
    }];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - NibAction

- (IBAction)addSchemeTap:(id)sender {
    NSString *url = [NSString stringWithFormat:@"nursing/wound/list/%@", self.patient.stringId];
    MBProgressHUD *hud = [[WCPopupLayerManager shareMg] showSimple:self.view];
    [[WCNetworkAccessManager sharedMg] GET:url parameters:nil success:^(NSDictionary *result){
        NSArray *data = result[@"data"];
        if (data.count > 0) {
            WCNCareSchemeVC *vc = [[WCNCareSchemeVC alloc] initWithPatient:self.patient withScheme:nil];
            vc.offerCareParts = data;
            [self.navigationController pushViewController:vc animated:YES];
        } else {
            [[WCPopupLayerManager shareMg] showTextOnly:@"请先为该患添加伤口部位" withView:self.navigationController.view];
        }
    } failure:nil netError:nil always:^{
        [hud hide:YES];
    }];
}

#pragma mark - <UITableViewDataSource, UITableViewDelegate>

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.schemes.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PatientScheme *scheme = self.schemes[indexPath.row];
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];
    
    [cell setSeparatorInset:UIEdgeInsetsZero];
    if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
        [cell setPreservesSuperviewLayoutMargins:NO];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    
    cell.imageView.image = [UIImage imageNamed:@"CaseCell_GreenHeader"];
    cell.textLabel.text = scheme.templateCaseName;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    PatientScheme *scheme = self.schemes[indexPath.row];
    
    WCNCareSchemeVC *vc = [[WCNCareSchemeVC alloc] initWithPatient:self.patient withScheme:scheme];
    [self.navigationController pushViewController:vc animated:YES];
}

@end
